<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>EU Projects Visualisation</title>

<link rel=stylesheet type=text/css href=http://acl.arts.usyd.edu.au/projects/networkmap/styles.css>
<style>
img.icon { margin-right: 4px; }
</style>

<script src=http://hapi.heuristscholar.org/load?v=02&key=ce1c992c139cfd5a6fdc77c5c71444925f229942></script>
<script src=http://hapi.heuristscholar.org/02/goi.js></script>

<script src="http://maps.google.com/maps?file=api&amp;v=2.105&amp;key=ABQIAAAAWXRbJ6-0zaNuAoz9XBmHZRTOHJksE5z9Wm_cd8XHFESSXyKZQhQ6Wa_KV6YE4tIo4Jn1cD3eEmpr8Q" type="text/javascript"></script>
<xscript src="main-105.js"></xscript>
<script>
var map, cacheMap;
window.console = window.firebug;

function calculateMidwayPoint(point1, point2, fraction) {
	/* calculate the point some fraction of the way between point1 and point2 */
	return new GLatLng(fraction * point2.lat() + (1-fraction) * point1.lat(), 
	                   fraction * point2.lng() + (1-fraction) * point1.lng());
}

function makeNewTimeoutFunction(center, zoomIn) {
	return function() {
		if (zoomIn) { 
			map_zoomIn(center.lat(), center.lng());
		}
		else {
			map_zoomOut(center.lat(), center.lng());
		}
	}
}






var flashmap;
function load() {
	if (! HCurrentUser.isLoggedIn()) {
		alert("You are not logged in to Heurist.\nRedirecting you now ...");
		location.replace("http://heuristscholar.org/heurist/php/login.php?last_uri=" + encodeURIComponent(location.href));
	}
	else if (! HCurrentUser.isInWorkgroup(HWorkgroupManager.getWorkgroupById(19))) {
		alert("You are not in the EUProject workgroup.\nSome records may not be visible to you.");
	}

	if (document.getElementById("map_object").setCrux) {
		flashmap = document.getElementById("map_object");
	}
	else {
		flashmap = document.getElementById("map_embed");
	}


	loadSearch("ids:"+initialID);
}

function loadSearch(searchString) {
	if (! searchString.match(/\S/)) {
		alert("Enter a search string");
		document.getElementById("q").focus();
		return;
	}

	HeuristScholarDB.loadRecords(new HSearch(searchString), new HLoader(function(search, records) {
		if (! records.length) {
			alert("No matching records");
			return;
		}

		for (var i=0; i < records.length; ++i) { processNewRecord(records[i]); }
		chooseRecord(records[0]);
	}));
}

</script>

</head>
<body class=mainpage onload="load();" style="margin-bottom: 0;">
<script>
var initialID = location.search.replace(/.*id=(\d+).*/, "$1") || 49886;
var chosenRecord = null;
var recordStack = [];	// a list of all the previous records that have been visited
var recordStackIndex = 0;

var relatedDepth = 0;
var knownRecordIDs = [], knownRecordIDsMap = {};
var XField = HDetailManager.getDetailTypeById(210);
var YField = HDetailManager.getDetailTypeById(211);
var geoField = HDetailManager.getDetailTypeById(230);
function processNewRecord(record) {
	var id = record.getID();
	var i;
	var x, y, geo;
	if (! knownRecordIDsMap[id]) {
		knownRecordIDsMap[id] = true;
		knownRecordIDs.push(id);
	}
}

// restrict all searches, loads etc to just the records with a kwd:18004
var BaseSearch = HSearch;
HAPI.Search = HSearch = function(query, options) {
	// don't reload known records
	var excludeIDs = "";
	if (knownRecordIDs) { excludeIDs = "-ids:" + knownRecordIDs.join(",") + " "; }
	BaseSearch.call(this, query+" "+excludeIDs, options);
//        BaseSearch.call(this, "kwd:18004 "+excludeIDs+query, options);
};
HSearch.getClass = function() { return "HSearch"; };
HAPI.inherit(HSearch, BaseSearch);


function chooseRecordCallback() {
	// Callback for GMarker: used with GEvent.bind such that (this) is an HRecord
	chooseRecord(this);
}


function chooseRecord(record, dontAlterStack) {
	var crt = document.getElementById("current-record-title");
	var crl = document.getElementById("current-record-link");
	var cri = document.getElementById("current-record-image");
	var rrList = document.getElementById("other-records");

	crt.innerHTML = "";
	rrList.innerHTML = "Loading ...";
	document.getElementById("loading-div").style.display = "block";

	crt.appendChild(document.createTextNode(record.getTitle()));
	crl.href = "http://heuristscholar.org/heurist-test/edit?bib_id=" + record.getID();
	cri.src = "http://heuristscholar.org/heurist-test/img/reftype/" + (record.getRecordType().getID() || "questionmark") + ".gif";
	cri.style.display = "";
	cri.style.verticalAlign = "baseline";

	chosenRecord = record;
	if (! dontAlterStack) {
		// The record stack keeps track of which records we've previously chosen,
		// but in order to navigate back and forth between records already on the stack we need an override
		// (see gotoPreviousRecord and gotoNextRecord)

		if (recordStackIndex+1 >= recordStack.length) {
			// easy case: we are already looking at the last record on the stack, push the new record on the end
			recordStack.push(chosenRecord);
		}
		else {
			// harder case: there are other records lurking on the stack ABOVE (not including) our current position,
			// and we want to replace those all with one single record (the new record)
			recordStack.splice(recordStackIndex+1, recordStack.length-recordStackIndex-1, chosenRecord);
		}
		recordStackIndex = recordStack.length-1;
	}

	relatedDepth = 0;
	if (! chosenRecord.relatedLoaded) {
		++relatedDepth;
//sm.loadRecords(new HSearch("relatedto:"+_id+" OR linkto:"+_id+" OR linkedto:"+_id), loader);
                //sm.loadRecords(new HSearch("relationsfor:"+_id), loader);

		HeuristScholarDB.loadRecords(new HSearch("kwd:18004 linkedto:"+chosenRecord.getID()+" OR kwd:18004 linkto:"+chosenRecord.getID()+" OR relatedto:"+chosenRecord.getID()),
		                             new HLoader(addAllRelatedLinks));

//		chosenRecord.loadRelated(HeuristScholarDB, new HLoader(addAllRelatedLinks));
		chosenRecord.relatedLoaded = true;
	}
	else {
		addAllRelatedLinks();
	}
}

function addAllRelatedLinks(search, recordSet) {
	map_clearMap();

	loadPointsAndLines(chosenRecord.getID());

	var i;
	if (search) {
		for (i=0; i < recordSet.length; ++i) { processNewRecord(recordSet[i]); }
	}
	relatedDepth = 0;

	var rrList = document.getElementById("other-records");
	rrList.innerHTML = "&nbsp;";
	document.getElementById("contentframe").style.height = (document.getElementById("contentcell").offsetHeight - 50) + "px";

	rrList.innerHTML = "";

	var relnType;
	var relations, relationsByType, recordTypes, relns;
	var list;
	var rec, recType, relType;

	relations = chosenRecord.getRelationships();
	if (relations.length > 0) {
		relationsByType = {};
		recordTypes = [];

		for (i=0; i < relations.length; ++i) {
			if (relations[i].getPrimaryRecord().getID() === chosenRecord.getID()) {
				rec = relations[i].getSecondaryRecord().getRecord();
				relType = relations[i].getType();
				recType = rec.getRecordType().getID();
			}
			else {
				rec = relations[i].getPrimaryRecord().getRecord();
				relType = relations[i].getInverseType();
				recType = rec.getRecordType().getID();
			}
			if (! rec) { continue; }

			if (relationsByType[recType]) {
				if (relationsByType[recType][relType]) {
					relationsByType[recType][relType].push(rec);
				}
				else {
					relationsByType[recType][relType] = [ rec ];
				}
			}
			else {
				recordTypes.push(recType);
				relationsByType[recType] = {};
				relationsByType[recType][relType] = [ rec ];
			}
		}

		recordTypes.sort(function(a,b){return a-b;});
		for (var j=0; j < recordTypes.length; ++j) {
		    recType = recordTypes[j];

		    rrList.appendChild(document.createElement("hr"));

		    for (relnType in relationsByType[recType]) {
			relns = relationsByType[recType][relnType];
			relns.sort(strcmp);

			var newDiv = document.createElement("div");
			newDiv.className = "relation_type";
			newDiv.appendChild(document.createTextNode(relnType));
			rrList.appendChild(newDiv);

			for (i=0; i < relns.length; ++i) {
				addRelatedRecordLink(rrList, relns[i]);
			}

			rrList.appendChild(document.createElement("br"));
		    }
		}
	}
	else {
		rrList.innerHTML = "<i>No related records</i>";
	}
}
function strcmp(a,b) { var x = a.getTitle(), y = b.getTitle(); return (x < y)? -1 : (x > y)? 1 : 0; }

function addRelatedRecordLink(rrList, record) {
	var newTable = document.createElement("table");
	newTable.className = "resource_table";
	var newTr = newTable.appendChild(document.createElement("tbody")).appendChild(document.createElement("tr"));

	var newTd1 = newTr.appendChild(document.createElement("td"));
	var newTd2 = newTr.appendChild(document.createElement("td"));

	var newA1 = newTd1.appendChild(document.createElement("a"));
	newA1.title = "View resource details";
	newA1.href = "http://heuristscholar.org/heurist-test/legacy/view.php?bib_id=" + record.getID();

	var newImg = newA1.appendChild(document.createElement("img"));
	newImg.src = "http://heuristscholar.org/heurist-test/img/reftype/" + (record.getRecordType().getID() || "questionmark") + ".gif";
	newImg.className = "icon";

	var newA2 = newTd2.appendChild(document.createElement("a"));
	newA2.className = "resource_link";
	newA2.title = "View related resources";
	newA2.href = "#";
	newA2.onclick = function() {
		chooseRecord(record);
		return false;
	};
	newA2.appendChild(document.createTextNode(record.getTitle()));

	rrList.appendChild(newTable);
}


function gotoPreviousRecord() {
	if (recordStackIndex === 0) { return; }	// no previous record

	var previousRecord = recordStack[--recordStackIndex];
	chooseRecord(previousRecord, /* dontAlterStack = */ true);
}
function gotoNextRecord() {
	if (recordStackIndex+1 >= recordStack.length) { return; }	// no "next" record

	var nextRecord = recordStack[++recordStackIndex];
	chooseRecord(nextRecord, /* dontAlterStack = */ true);
}



function plotMap() {
	var i, j, k;
	var recordType;
	var records;
	var marker;

	// clear any existing markers
	map_clearMap();
	map_setCrux(chosenRecord.locations[0].lat(), chosenRecord.locations[0].lng(), chosenRecord.getTitle());

	records = chosenRecord.getRelatedRecords();
	for (i=0; i < chosenRecord.locations.length; ++i) {
		// if (chosenRecord.locations[i].depth > 0) { continue; }	// an inherited location
		for (j=0; j < records.length; ++j) {
			if (! records[j].locations) { continue; }
			for (k=0; k < records[j].locations.length; ++k) {
				if (records[j].locations[k].depth > 1) { continue; }	// an inherited location
				if (chosenRecord.locations[i].equals(records[j].locations[k])) { continue; }
				map_addRelation(records[j].locations[k].lat(), records[j].locations[k].lng(), records[j].getTitle());
console.log([i, j, k]);
			}
		}
	}

	var center = bounds.getCenter();
	map_panTo(center.lat(), center.lng());
}


function moveEndCallback() {
	document.getElementById("loading-div").style.display = "none";
}


function pointsCallback(vals) {
	var record;
	var i;
	var point;

	bounds = new GLatLngBounds();
	for (var bibID in vals) {
		record = HeuristScholarDB.getRecord(bibID);
		if (! record) { continue; }

		record.locations = [];
		for (i=0; i < vals[bibID].length; ++i) {
			point = new GLatLng(parseFloat(vals[bibID][i][1]), parseFloat(vals[bibID][i][0]));
			point.depth = vals[bibID][i][2];
			record.locations.push(point);
			bounds.extend(point);
		}
	}

	plotMap();
}


function loadPointsAndLines(id) {
	HAPI.XHR.sendRequest("http://hapi.heuristscholar.org/get_points.php?bib_id=" + id, pointsCallback);
}

function map_clearMap() { flashmap.clearMap(); }
function map_setCrux(lat, lng, name) { flashmap.setCrux(lat, lng, name); }
function map_addRelation(lat, lng, name) { flashmap.setCrux(lat, lng, name); }
function map_zoomOut(lat, lng) { flashmap.zoomIn(lat, lng); }
function map_zoomIn(lat, lng) { flashmap.zoomIn(lat, lng); }
function map_panTo(lat, lng) { flashmap.panTo(lat, lng); }

</script>

<div id=container>
 <div id=banner></div>
 <div id=center>
  <div id=mapcontainer>
   <div id=loading-div style="position: absolute; width: 500px; z-index: 100000;"><img src=../loading-animation.gif style="position: absolute; right: 10px; top: 10px;"></div>

    <object id=map_object
      classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
      codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0"
      width="500px"
      height="640px">
      <param name="movie" value="relationsmapper.swf">
      <param name="quality" value="high">
      <embed id=map_embed
swliveconnect="true"
        width="500px"
        height="640px"
        src="relationsmapper.swf"
        quality="high"
        pluginspage="http://www.macromedia.com/go/getflashplayer"
        type="application/x-shockwave-flash">
      </embed>
    </object>

   <div id=map style="width: 500px; height: 640px;"></div>
   <div id=cache-map style="position: absolute; visibility: hidden; top: -1000px; width: 500px; height: 640px;"></div>
<!--
  		<applet codebase="http://acl.arts.usyd.edu.au/map2" 
  			code="org.alov.viewer.SarApplet" 
			name="mapApplet"        
			archive="TMJava.jar"  
			width="500"  height="640" 
			align="right">
   			 <Xparam name="pid" value="20505">
   			 <param name="pid" value="http://acl.arts.usyd.edu.au/projects/networkmap/euprojects/worldmap.tms">
   			 <param name="debug" value="3">
   			 <param name="target" value="contentframe">
   			 <param name="maxsidsize" value="100000">

    			<param name="layout" value="http://acl.arts.usyd.edu.au/projects/networkmap/euprojects/layout.xml">
  		</applet>
-->
  </div>
 </div>
 <div id=right>
  <table border=0 cellpadding=0 cellspacing=0 style="width: 100%; height: 100%;"><tbody>
   
   <tr><td style="height: 20px;">
    <h2>
     <a href=# onclick="gotoPreviousRecord(); return false;" style="float: left; padding: 0 1ex;">back</a>
     <a href=# onclick="gotoNextRecord(); return false;" style="float: left; padding: 0 1ex;">forward</a>
     Resources
    </h2>
   </td></tr>

<!--
   <tr><td style="height: 20px; padding: 0 1em;">
    <form onsubmit="var q = document.getElementById('q').value; loadSearch('relatedto: '+q); return false;" style="float: right;">
     <b>Search: </b>
     <input type="text" style="border: 1px solid gray; width: 30ex;" id="q">
     <input type="submit" value="go" style="font-weight: bold;">
    </form>
   </td></tr>
-->
    <tr><td style="height: 20px;">
    <table class=resource_table><tr>
     <td><h3><a id=current-record-link title="View resource details" href=#
            onclick="window.open(this.href, '', 'status=1,scrollbars=1,width=600,height=500'); return false;"><img id=current-record-image style="display: none;"></a>
         &nbsp;</h3>
     </td>
     <td>
      <h3 id=current-record-title></h3>
     </td>
    </tr></table>
  </td></tr>


  <tr><td style="vertical-align: top;" id="contentcell">
  <div id=contentframe style="overflow: auto;">
   <div class=content>
    <div id=other-records></div>
   </div>
  </div>
  </td></tr>
 </tbody></table>
 </div>
</div>

</body>
</html>
